package SMS.controller;

import SMS.model.Res;
import SMS.pojo.Books;
import SMS.service.BookService;
import SMS.vo.IId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@CrossOrigin
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;

    @GetMapping("/list")
    @ResponseBody
    public Res<List<Object>> list(@RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
                                  @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        // 查询图书总数
        int totalCount = bookService.getTotalCount();
        List<Books> books =  bookService.list(currentPage, pageSize);
        // 封装数据
        Map<String, Object> data = new HashMap<>();
        data.put("books", books);
        data.put("totalCount", totalCount);

        Res<List<Object>> res = new Res(200, "success", data);
        return res;
    }

    //    更新书籍信息
    @PostMapping ("/update")
    @ResponseBody
    public Res update(@RequestBody Books book) {
//        System.out.println("controller:"+book);
        // 更新书籍信息，假设 bookService 处理更新逻辑
        bookService.updateBook(book);
        return new Res(200, "success", null);
    }

    //    删除书籍数据
    @PostMapping("/delete")
    @ResponseBody
    public Res delete(@RequestBody IId req) {
        // 删除书籍信息，假设 bookService 处理删除逻辑
        bookService.deleteBook(req.id);
        return new Res(200, "success", null);
    }
    //    添加书籍数据
    @PostMapping("/add")
    @ResponseBody
    public Res add(@RequestBody Books book) {
        // 添加书籍信息，假设 bookService 处理添加逻辑
        bookService.addBook(book);
        return new Res(200, "success", null);
    }

    //    按学号或姓名查询书籍数据
    @GetMapping ("/search")
    @ResponseBody
    public Res search(@RequestParam(value = "keyword") String req,
                      @RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
                      @RequestParam(value = "pageSize", defaultValue = "10")int pageSize) {
        // 假设req是书籍的学号，按学号查询书籍数量
        int totalCount = bookService.countByBookNo(req);
        // 按学号查询不到任何书籍
        if (totalCount == 0){
            totalCount = bookService.countByBookName(req);
            // 按姓名查询不到任何书籍
            if (totalCount == 0){
                return new Res(400, "学号或姓名错误或该书籍不存在", null);
            }
            // 按姓名查询到书籍
            else {
                // 查询书籍信息，假设 bookService 返回一个书籍列表
                List<Books> books = bookService.searchBookByName(req, currentPage, pageSize);
                // 封装数据
                Map<String, Object> data = new HashMap<>();
                data.put("books", books);
                data.put("totalCount", totalCount);

                return new Res(200, "success", data);
            }
        }
        // 按学号查询到书籍
        else {
            // 查询书籍信息，假设 bookService 返回一个书籍列表
            List<Books> books = bookService.searchBookByNo(req,currentPage, pageSize);
            // 封装数据
            Map<String, Object> data = new HashMap<>();
            data.put("books", books);
            data.put("totalCount", totalCount);
            return new Res(200, "success", data);
        }
    }
}
